﻿

using VaccineHost.Extend;
using VaccineHost.Model;

namespace VaccineHost.Services
{
    /// <summary>
    /// 疫苗信息服务
    /// </summary>
    public class VaccineService: IVaccineService
    {
        private readonly HttpClient _httpClient;

        public VaccineService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        public async Task<OpResult> Create(CreateVaccineInfoInput createInput)
        {
            throw new NotImplementedException();
            var request = new HttpRequestMessage(HttpMethod.Post, "api/v1/vaccination/info")
            {
                Content = JsonContent.Create(createInput)
            };

            var response = await _httpClient.SendAsync(request);

            var regresult = await response.ToResult();
            if (regresult is OpResult)
            {
                return regresult as OpResult;
            }
            else
            {
                OpResult result = new OpResult();
                result.Code = 200;
                result.Message = "新增成功。";

                return result;
            }
        }

        /// <summary>
        /// 获取疫苗
        /// </summary>
        /// <returns></returns>
        public async Task<List<VaccineDto>> GetVaccines()
        {
            var getmsg = await _httpClient.GetAsync("api/v1/vaccine/info/all");
            var regresult = await getmsg.ToResult();
            if (regresult == null || regresult is OpResult)
            {
                return new List<VaccineDto>();
            }
            else
            {
                return regresult.ToString().ToObject<List<VaccineDto>>();
            }
        }

        public async Task<OpResult> Update(UpdateVaccineInfoInput updateInput)
        {
            var request = new HttpRequestMessage(HttpMethod.Put, "api/v1/vaccination/info")
            {
                Content = JsonContent.Create(updateInput)
            };

            var response = await _httpClient.SendAsync(request);

            var regresult = await response.ToResult();
            if (regresult is OpResult)
            {
                return regresult as OpResult;
            }
            else
            {
                OpResult result = new OpResult();
                result.Code = 200;
                result.Message = "更新成功。";

                return result;
            }
        }
    }
}
